from google.appengine.ext import db
from google.appengine.api import users
import datetime
import logging

#Contact data model
class ContactsModel(db.Model):
    Title	= db.StringProperty()
    Name 	= db.StringProperty(required=True)
    BirthDate	= db.DateTimeProperty()
    MobilePhone	= db.StringProperty()
    Email	= db.StringProperty()
    CreatedBy	= db.UserProperty(required=True)
    CreatedOn	= db.DateTimeProperty(auto_now_add=True)
    UpdatedOn	= db.DateTimeProperty(auto_now=True)
    UpdatedBy	= db.UserProperty()


def addContact(vTitle, vName, vBirthDate, vMobilePhone, vEmail, user):
    """
    Add a new Contact to the datastore.
  
    Returns  
	The id of the Contact or None if the add failed.
    """
    logging.info('Start addContact request')
    if user is None:
	return
  
    try:
	now = datetime.datetime.now()
	nContact = ContactsModel(
	    Title	= vTitle,
	    Name 	= vName,
	    BirthDate	= vBirthDate,
	    MobilePhone	= vMobilePhone,
	    Email	= vEmail,
	    CreatedBy	= user,
	    CreatedOn	= now
	)
	nContact.put()
	logging.info('End addContact successfully')
	return nContact.key().id()
    except db.Error:
	logging.error('Error: addContact')
	return None



def updateContact(ContactID, vTitle, vName, vBirthDate, vMobilePhone, vEmail, user):
    """
    Update an Contact
    
    Returns the Contact ID or None if an error occurred
    """
    logging.info('Start updateContact')
    
    if user is None:
      return

    try:
	def transact():
	    now = datetime.datetime.now()
	    currContact	=ContactsModel.get_by_id(int(ContactID))
	    currContact.Title		= vTitle
	    currContact.Name 		= vName
	    currContact.BirthDate	= vBirthDate
	    currContact.MobilePhone	= vMobilePhone
	    currContact.Email		= vEmail
	    currContact.UpdatedBy	= user
	    currContact.UpdatedOn	= now
	    currContact.put()
	db.run_in_transaction(transact)
	
	logging.info('End updateContact successfully')
	return ContactID
    except db.Error:
	logging.error('Error: upateContact')
	return None
    


def deleteContact(ContactID, user):
    """
    Delete an Contact
    
    Returns  
      1 or 0 denpending on success
    """
    logging.info('Start deleteContact ID=: %s' %ContactID)
    if user is None:
      return 0

    try:      
	def transact():
	    currContact = ContactsModel.get_by_id(int(ContactID))
	    currContact.delete()
	db.run_in_transaction(transact)
	
	logging.info('End deleteContact successfully ID=: %s' %ContactID)
	return 1
    except db.Error:
	logging.error('#Error deleteContact ID= %s' %ContactID)
	return 0



def getContacts(user):
    """ 
    Returns  the Contacts list
    """
    if user is None:
      return None
    
    try:
	Contacts = ContactsModel.gql("")
  
	return Contacts
    except db.Error:
	return None


def getContact(ContactID, user):
    """ 
    Returns the Contact with the ID = ContactID
    """
    if user is None:
      return  
    logging.info('Start getContact: %s' %ContactID)
    try:
	oContact=ContactsModel.get_by_id(int(ContactID))
	#logging.info('End getContact successfully: %s' %ContactID)
	return oContact
    except db.Error:
	logging.error('#Error getContact ID= %s' %ContactID)
	return None
    
    
def getID(Name, user):
    """ 
    Returns the ID 
    """
    if user is None:
      return  
    
    try:
	oContact=ContactsModel.gql("WHERE Name = :1", Name).get()	
	return oContact.key().id()
    except db.Error:
	logging.error('#Error getID ID')
	return None

def getKey(Name, user):
    """ 
    Returns the ID 
    """
    if user is None:
      return  
    
    try:
	oContact=ContactsModel.gql("WHERE Name = :1", Name).get()
	return oContact.key()
    except db.Error:
	logging.error('#Error getKey Key')
	return None
